package ccmis.finance.capital.dao;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.QueryHint;

import jfox.platform.infrastructure.EntityObject;
import jfox.platform.infrastructure.SuperDAO;
import jfox.platform.organization.dao.CompanyDAOBean;
import jfox.platform.organization.entity.Company;

import org.jfox.entity.QueryExt;

import ccmis.finance.capital.entity.LendContract;

/**
 * @author <a href="mailto:ren.yingb@gmail.com">Ren Yingbo</a>
 */
@Stateless
@Local
@NamedNativeQueries(
		{
			@NamedNativeQuery(
					name = LendContractDAOBean.GET_LENDCONTRACT_BY_ID,
					query = "SELECT * FROM T_FINANCIAL_LEND_CONTRACT WHERE ID=$ID AND VSTATUS=" + EntityObject.VSTATUS_NORMAL,
					resultClass = LendContract.class,
					hints = {
					@QueryHint(name = "cache.partition", value = "function")
					        }
			),
			@NamedNativeQuery(
					name = LendContractDAOBean.GET_ALL_LENDCONTRACTS,
					query = "SELECT * FROM T_FINANCIAL_LEND_CONTRACT WHERE VSTATUS=" + EntityObject.VSTATUS_NORMAL + " ORDER BY ID",
					resultClass = LendContract.class,
					hints = {
					@QueryHint(name = "cache.partition", value = "function")
					        }
			),
			@NamedNativeQuery(
					name = LendContractDAOBean.INSERT_LENDCONTRACT,
					query = "INSERT INTO T_FINANCIAL_LEND_CONTRACT(" +
					        "ID," +
					        "NAME, " +
                            "PRIORITY, " +
                            "VSTATUS, " +
                            "LEND_DEPT_ID," +
                            "LEND_TYPE_ID," +
                            "CNO," +
                            "PLAN_LEND_DATE," +
                            "PLAN_RETURN_DATE," +
                            "YEAR_LIMIT," +
                            "MONTH_LIMIT," +
                            "CVALUE)" +
                            "VALUES(" +
                            "$LENDCONTRACT.getId()," +
                            "$LENDCONTRACT.getName()," +
                            "$LENDCONTRACT.getPriority()," +
                            "$LENDCONTRACT.getVstatus()," +
                            "$LENDCONTRACT.getLendDeptID()," +
                            "$LENDCONTRACT.getLendTypeID()," +
                            "$LENDCONTRACT.getCNo()," +
                            "$LENDCONTRACT.getPlanLendDate()," +
                            "$LENDCONTRACT.getPlanReturnDate()," +
                            "$LENDCONTRACT.getYearLimit()," +
                            "$LENDCONTRACT.getMonthLimit()," +
                            "$LENDCONTRACT.getCValue() )",
                    hints = {
                    @QueryHint(name = "cache.partition", value = "function")
                            }
			),
			@NamedNativeQuery(
					name = LendContractDAOBean.DELETE_LENDCONTRACT,
					query = "UPDATE T_FINANCIAL_LEND_CONTRACT SET VSTATUS = " + EntityObject.VSTATUS_REMOVED + " WHERE ID=$ID",
					resultClass = LendContract.class,
					hints = {
					@QueryHint(name = "cache.partition",value = "function")
					        }
			),
			@NamedNativeQuery(
					name = LendContractDAOBean.UPDATE_LENDCONTRACT,
					query = "UPDATE T_FINANCIAL_LEND_CONTRACT" +
					        " SET NAME = " + "$LENDCONTRACT.getName()," +
                            " PRIORITY = " + "$LENDCONTRACT.getPriority()," +
                            " VSTATUS = " + "$LENDCONTRACT.getVstatus()," +
                            " LEND_DEPT_ID = " + "$LENDCONTRACT.getLendDeptID()," +
                            " LEND_TYPE_ID = " + "$LENDCONTRACT.getLendTypeID()," +
                            " CNO = " + "$LENDCONTRACT.getCNo()," +
                            " PLAN_LEND_DATE = " + "$LENDCONTRACT.getPlanLendDate()," +
                            " PLAN_RETURN_DATE = " + "$LENDCONTRACT.getPlanReturnDate()," +
                            " YEAR_LIMIT = " + "$LENDCONTRACT.getYearLimit()," +
                            " Month_LIMIT = " + "$LENDCONTRACT.getMonthLimit()," +
                            " CVALUE = " + "$LENDCONTRACT.getCValue()" +
                            " WHERE ID=" + "$LENDCONTRACT.getId()",
                    hints = {
                	@QueryHint(name = "cache.partition",value = "function")
                			}
			)
		}
		
)
public class LendContractDAOBean extends SuperDAO implements LendContractDAO {

	public final static String GET_LENDCONTRACT_BY_ID = "getLendContractById";
	public final static String GET_ALL_LENDCONTRACTS = "getAllLendContract";
	public final static String INSERT_LENDCONTRACT = "insertLendContract";
	public final static String DELETE_LENDCONTRACT = "deleteLendContract";
	public final static String UPDATE_LENDCONTRACT = "updateLendContract";
	
	public int deleteLendContract(long id) {
		Map<String,Object> params = new HashMap<String,Object>(1);
		params.put("ID", id);
		return executeNamedNativeUpdate(DELETE_LENDCONTRACT,params);	
	}

	public List<LendContract> getAllLendContracts() {
		return (List<LendContract>)processNamedNativeQuery(GET_ALL_LENDCONTRACTS, Collections.EMPTY_MAP);
	
	}

	public LendContract getLendContractById(long id) {
		return (LendContract)getEntityObjectByColumn(GET_LENDCONTRACT_BY_ID,"ID",id);
	
	}

	public void insertLendContract(LendContract lendContract) {
		Map<String,Object> params = new HashMap<String,Object>(1);
		params.put("LENDCONTRACT", lendContract);
		executeNamedNativeUpdate(INSERT_LENDCONTRACT,params);
		
	}

	public int updateLendContract(LendContract lendContract) {
		Map<String,Object> params = new HashMap<String,Object>(1);
		params.put("LENDCONTRACT", lendContract);
		return executeNamedNativeUpdate(UPDATE_LENDCONTRACT,params);

	}

}
